package com.example.my_self_view

import android.animation.ObjectAnimator
import android.app.Activity
import android.os.Bundle
import android.view.View
import android.view.animation.DecelerateInterpolator
import android.widget.Button
import java.util.concurrent.TimeUnit
import kotlin.time.Duration.Companion.seconds

class MainActivity: Activity() {

    private lateinit var stepView: QQStepView
    private lateinit var colorTrackTextView: ColorTrackTextView
    private var leftCurrentProcess = 0.0f
    private var rightCurrentProcess = 0.0f

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity_layout)

        initColorTrackTextView()

    }

    private fun initColorTrackTextView() {
        colorTrackTextView = findViewById(R.id.color_track_textview)
        findViewById<Button>(R.id.left_to_right).setOnClickListener {
            leftToRight()
        }
        findViewById<Button>(R.id.right_to_left).setOnClickListener {
            rightToLeft()
        }
    }

    private fun leftToRight() {
        colorTrackTextView.setDirection(ColorTrackTextView.Direction.LEFT_TO_RIGHT)

        val animator = ObjectAnimator.ofFloat(leftCurrentProcess, 1f)
        animator.duration = TimeUnit.SECONDS.toMillis(2)
        animator.addUpdateListener {
            leftCurrentProcess = animator.animatedValue as Float
            rightCurrentProcess = 1f - leftCurrentProcess
            colorTrackTextView.setCurrentProgress(leftCurrentProcess)
        }
        animator.start()
    }

    private fun rightToLeft() {
        colorTrackTextView.setDirection(ColorTrackTextView.Direction.RIGHT_TO_LEFT)

        val animator = ObjectAnimator.ofFloat(rightCurrentProcess, 1f)
        animator.duration = TimeUnit.SECONDS.toMillis(2)
        animator.addUpdateListener {
            rightCurrentProcess = animator.animatedValue as Float
            leftCurrentProcess = 1f - rightCurrentProcess
            colorTrackTextView.setCurrentProgress(rightCurrentProcess)
        }
        animator.start()
    }

    private fun initQQStepView() {
//        stepView = findViewById(R.id.qq_step_view)
//        stepView.setStepMax(4000)
//
//        // 属性动画
//        val animator = ObjectAnimator.ofFloat(0f, 3000f)
//        animator.duration = 1000L
//        animator.interpolator = DecelerateInterpolator()
//        animator.addUpdateListener {
//            val currentStep = (animator.animatedValue) as Float
//            stepView.setCurrentStep(currentStep.toInt())
//        }
//        animator.start()
//
//        stepView.setOnClickListener {
//            animator.start()
//        }
    }
}